package com.example.homework.recycler

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.RecyclerView
import com.example.homework.R
import kotlinx.android.synthetic.main.activity_recycler.*
import kotlinx.android.synthetic.main.item_my_recycler.view.*

class RecyclerActivity : AppCompatActivity() {

    private lateinit var mAdapter: MyAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_recycler)

        var dataList:MutableList<MyBean> = mutableListOf()
        for (i in 0..5){
            for (j in 0..10){
                dataList.add(MyBean("分组$i-条目$j","分组$i"))
            }
        }
        mAdapter = MyAdapter(dataList)
        rv.addItemDecoration(MyDecoration(this))
        rv.adapter = mAdapter
    }


    class MyAdapter(var mData:List<MyBean> = emptyList()) : RecyclerView.Adapter<MyHolder>(){

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyHolder {

            return MyHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_my_recycler,parent,false))

        }

        override fun getItemCount(): Int = mData.size

        override fun onBindViewHolder(holder: MyHolder, position: Int) {
            holder.tv?.text = mData[position].name
        }

        fun isGroupHeader(position: Int): Boolean {
            if (position == 0) return true
            return mData[position].groupName != mData[position-1].groupName
        }

    }


    class MyHolder(itemView :View) : RecyclerView.ViewHolder(itemView){

        var tv:TextView? = null
        init {
            tv = itemView.tv
        }

    }

}